package com.rq.base.ui;


import android.app.Activity;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.rq.base.ui.common.CommonFragment;
import com.rq.base.util.LOG;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.rq.base.ui.BaseController.TAG_PASS;
import static com.rq.base.ui.BaseController.TAG_PASS_DATA_SIZE;

public class FragmentContainer implements BaseController.Operator {
    List<BaseFragment> list = new ArrayList<>();
    BaseController baseController;
    int bindId;


    public void attach(BaseController ctr, int fra_1, Class<? extends BaseController> clazz, Serializable... pass) {
        this.list.add(CommonFragment.instance(clazz, pass));
        this.baseController = ctr;
        this.bindId = fra_1;
        ctr.setData2View(fra_1, this);
    }

    public BaseFragment getLastFra() {
        return list.get(list.size() - 1);
    }

    @Override
    public void finish(BaseController controller) {
        if (list.size() > 0) {
            FragmentTransaction mTransaction = controller.getActivity().getSupportFragmentManager().beginTransaction();
            Fragment fragment = list.remove(list.size() - 1);
            mTransaction.hide(fragment);
            mTransaction.commitAllowingStateLoss();
        }
    }

    @Override
    public void finishOK(BaseController controller, Serializable... back) {
        if (back != null && list.size() > 0) {
            LOG.d("FragmentContainer", "finishOK.451:TAG_PASS");
            Intent intent = new Intent();
            for (int i = 0; i < back.length; i++) {
                intent.putExtra(TAG_PASS + i, back[i]);
            }
            intent.putExtra(TAG_PASS_DATA_SIZE, back.length);
            for (int i = 0; i < list.size(); i++) {
                LOG.e("FragmentContainer", i + ".finishOK:" + list.get(i).getController().getClass().getSimpleName());
            }
            if (list.size() > 1) {
                BaseFragment fragment = list.get(list.size() - 2);
                LOG.d("FragmentContainer", fragment.getController().getClass().getSimpleName() + ".finishOK.TAG_PASS_DATA_SIZE:" + back.length);
                fragment.onActivityResult(controller.getRequestCode(), Activity.RESULT_OK, intent);
            }
        }
        finish(controller);
    }

    public void open(Class<? extends BaseController> clazz, Serializable... pass) {
        this.list.add(CommonFragment.instance(clazz, pass));
        baseController.setData2View(bindId, this);
    }

    @Override
    public <P extends BaseController> boolean skip(BaseController controller, Class<P> clazz, Serializable... pass) {
        if (list.size() > 0) {
            FragmentTransaction mTransaction = controller.getActivity().getSupportFragmentManager().beginTransaction();
            mTransaction.remove(list.remove(list.size() - 1));
            mTransaction.commitAllowingStateLoss();
        }
        open(clazz, pass);
        return true;
    }

    @Override
    public <P extends BaseController> boolean open(BaseController controller, Class<P> clazz, Serializable... pass) {
        open(clazz, pass);
        return true;
    }

    @Override
    public <P extends BaseController> boolean openFor(BaseController controller, Class<P> clazz, Serializable... pass) {
        open(clazz, pass);
        return true;
    }

    @Override
    public <P extends BaseController> boolean openWith(BaseController controller, Class<P> clazz, int requestCode, Serializable... pass) {
        this.list.add(CommonFragment.instanceWith(clazz, requestCode, pass));
        LOG.e("FragmentContainer", controller.getClass().getSimpleName() + ".98:");
        controller.setInnerPassCode(requestCode);
        baseController.setData2View(bindId, this);
        return true;
    }
}
